package cvc.framework.service;

import cvc.framework.entity.Camera;
import cvc.framework.mapper.CameraMapper;
import cvc.framework.webSocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

@Service
public class CameraService {
    //    String ip = "http://10.1.161.213:8083/";
    String ip = "http://58.34.215.18:3006/";
    //    String imgPath = "E:\\工作\\123";
    String imgPath = "/var/www/html";
    @Autowired
    private CameraMapper cameraMapper;

    public Camera getCamera() {
        return cameraMapper.getCamera();
    }

    public int updateCamera(Camera camera) {
        int cameraId = camera.getCameraId();
        int state = cameraMapper.updateCamera(camera);
//        System.out.println(state);
        if (state == 1) {
            boolean ws = WebSocket.sendMessageToUser("" + cameraId, "cameraConfig#" + camera.toString());
            if (!ws) {
                return 0;
            }
        } else {
            return 0;
        }
        return 1;
    }

    public String getImgPath() {
        File path = new File(imgPath);
        File[] files = path.listFiles();
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                return (int) (o2.lastModified() - o1.lastModified());
            }
        });
        String imgSrc = ip + files[0].getName();
        return imgSrc;
    }

}
